import { transformTime1, timeTransformData, transformCount } from '@/utils/util'
import { getAlbumSongs } from '@/api/artists/index'

export const transSingerInfo = (res) => {
  const { data: { artist, hotSongs } } = res
  const { id, name, briefDesc, picUrl, alias, musicSize, albumSize, mvSize } = artist
  const aliasStr = alias.length ? alias.join('|') : ''
  const singerInfo = {
    id,
    name,
    briefDesc,
    picUrl,
    musicSize,
    albumSize,
    mvSize,
    aliasStr
  } as SingerInfo
  const hotSongsList = hotSongs.map(item => {
    const { id, dt, name, alia, mv } = item
    const duration = transformTime1(dt)
    const aliasStr = alia.length ? '(' + alia.join('|') + ')' : ''
    return {
      id,
      duration,
      name,
      aliasStr,
      mv
    } as PlaylistSongDetail
  })
  return {
    singerInfo,
    hotSongsList
  }
}

export const transAlbums = async (res) => {
  const { data: { hotAlbums, more } } = res
  const hotAlbumsList = hotAlbums.map(item => {
    const { id, name, picUrl, publishTime } = item
    const pub = timeTransformData(publishTime)
    return {
      id,
      name,
      picUrl,
      pub
    }
  })
  for (let i = 0; i < hotAlbumsList.length; i++) {
    const { id } = hotAlbumsList[i]
    const { data: { songs } } = await getAlbumSongs(id)
    const songsList = []
    for (let j = 0; j < songs.length; j++) {
      const song = songs[j]
      const { id, dt, name, alia, mv } = song
      const duration = transformTime1(dt)
      const aliasStr = alia.length ? '(' + alia.join('|') + ')' : ''
      songsList.push({
        id,
        duration,
        name,
        aliasStr,
        mv
      } as PlaylistSongDetail)
    }
    hotAlbumsList[i].songsList = songsList
  }
  return {
    hotAlbumsList,
    more
  }
}

export const transMvs = (res) => {
  const { data: { mvs, hasMore } } = res
  const mvsList = mvs.map(item => {
    const { id, name, duration, imgurl, playCount } = item
    const dt = transformTime1(duration)
    const count = transformCount(playCount)
    return {
      id,
      name,
      duration: dt,
      playCount: count,
      imgurl
    }
  })
  return {
    mvs: mvsList,
    hasMore
  }
}
